
* === Robustness: Only keeping hhs with WD LESS THAN 5 kms =====================

/* This dataset is made in ${dofiles}05b_project_specific_data_incl_short_wd.do */
use ${newdata}carownership_dataset_bergen_shortwd.dta, clear

/* Only keep required obs */
keep if $trmgroup != 0 
keep if $trmgroup != .
keep if 	(year >= $firstyearpre  & year <= $lastyearpre) ///
		  | (year >= $firstyearpost & year <= $lastyearpost)

/* setup variables */
capt drop bergen 		  
gen commuters = ($trmgroup == 1 | $trmgroup == 3) 
gen bergen = 	($trmgroup == 1 | $trmgroup == 2)
gen post = (year >= $firstyearpost & year <= $lastyearpost)
gen bergen_commuters = bergen * commuters
gen commuters_post = commuters * post
gen bergen_commuters_post = bergen_commuters * post

/* All time-varying variables based on geography (residence or work location)
   are set to their 2014 values (variation post 2014 is endogenous to treatment) */
foreach var in dist time_work PublicVSCarTime_fam_mean PublicDiffCarTime_fam_mean grk {
	replace `var' = . if year != 2014
	bysort familienr (`var'): replace `var' = `var'[_n-1] if missing(`var')
}

sort familienr year
egen long famid_num = group(familienr)

/* === only keep households with work distance less than 5 kms ============== */
keep if dist <= 5

/* === Run regressions ====================================================== */
eststo clear
foreach regvar in bev nrbev ice cars {
	global yvar `regvar'

	reghdfe $yvar ///
		bergen_commuters_post /// DiDiD: post commuter in bergen	
		$xvar ///
		i.commuters#i.year /// 
		bergen_commuters /// 
		, absorb( ///
			i.grk#i.year ///
			i.famid_num ///
		) vce(cluster grk)		
	
	eststo reg_${yvar}
	summarize $yvar if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
	estadd scalar MeanValue1 = r(mean), replace
	summarize $yvar if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
	estadd scalar MeanValue2 = r(mean), replace
	eststo reg_${yvar} 
	* Saving estimates to ster files	
	estimates save ${ster}TableD2_${yvar}, replace	
}

/* === Reading regression results from ster files, saving to .tex file */
eststo drop *

foreach var in bev nrbev ice cars {
	estimates use "${ster}TableD2_`var'" 
	eststo
}
esttab * ///
	using "${tables}tabD2est.tex", ///
	keep(bergen_commuters_post)  ///
	coeflabels( ///
	bergen_commuters_post "Post $\times$ Paying commuters $\times$ Bergen" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

esttab * ///
	using "${tables}tabD2sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(N  MeanValue1 MeanValue2, ///
	label("Observations"  "Mean depvar 2014 (paying commuters, Bergen)" "Mean depvar 2017 (paying commuters, Bergen)" ///
	) fmt(0 4 4) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)